package com.sanss.log_plat.controller;

import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
public class IndexController {
    private RestTemplate restTemplate = new RestTemplate();

    @GetMapping("/index")
    @ResponseBody
    public String putIndex(){
        HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<String>("{}",headers);
        ResponseEntity<String> responseEntity = restTemplate.exchange("http://localhost:9200/test/", HttpMethod.PUT,
                entity,String.class);
        return responseEntity.getBody();
    }

    @GetMapping("/indexs")
    @ResponseBody
    public String getIndexs(){
        String reqString =
                "{}";

        HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<String>(reqString,headers);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(
                "http://localhost:9200/_cat/indices?v", String.class);
        return responseEntity.getBody();
    }

    @GetMapping("/putmapping")
    @ResponseBody
    public String putMapping(){
        String mapping = "{\n" +
                "  \"properties\": {\n" +
                "    \"name\": {\n" +
                "      \"type\": \"string\"\n" +
                "    }\n" +
                "  }\n" +
                "}";
        HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<String>(mapping,headers);
        ResponseEntity<String> responseEntity = restTemplate.exchange(
                "http://localhost:9200/test/_mapping/user",HttpMethod.PUT,entity, String.class);

        return responseEntity.getBody();
    }

    @GetMapping("/getMapping")
    @ResponseBody
    public String getMapping(){
        HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<String>("",headers);
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity(
//                "http://172.16.53.132:9200/_cat/indices?v", String.class);

        ResponseEntity<String> responseEntity = restTemplate.getForEntity(
                "http://localhost:9200/test/_mapping/user/?pretty", String.class);
        System.out.println(responseEntity.getBody());
        return responseEntity.getBody();
    }


}
